package com.glory.dgcpcpda.activity.recipe;

import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.StrzycxEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: PERecipeActivity
 * Author: XJH
 * Date: 2020/5/14 11:17
 * Description:
 * History:
 */
public class ZyRecipeActivity extends BaseActivity {
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.scrollView)
    NestedScrollView scrollView;

    @BindView(R.id.etLotIDzy)
    FreeUI_ClearEditText etLotID;

    @BindView(R.id.zyBDID)
    TextView zybdId;

    @BindView(R.id.zyRecipeName)
    TextView zyRecipeName;

    @BindView(R.id.zyEqpwork)
    FreeUI_ClearEditText zyeqpWork;

    @BindView(R.id.zyrbA)
    RadioButton rbA;

    @BindView(R.id.zyrbB)
    RadioButton rbB;

    @BindView(R.id.zyDownLoad)
    TextView zyDownload;


    private String mStationID;
    private String Lotid;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_zycx;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("GD1775下载测试程序");

        etLotID.requestFocus();

        // 先清除文本框的内容
        zybdId.setText("");
        zyRecipeName.setText("");

        initEdit(etLotID, s -> LotUtils.searchLotInfoStr(ZyRecipeActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<StrzycxEntity>() {
            @Override
            public void onSuccess(StrzycxEntity response) {
                etLotID.setText(s.toUpperCase());
                Lotid = s.toUpperCase();
                zybdId.setText(response.getRECIPEID());
                zyRecipeName.setText(response.getRECIPENAME());
                zyeqpWork.requestFocus();
            }

            @Override
            public void onFail(Throwable t) {
                //loadError(t, "LOTQUERY");
            }
        }));

        // 初始化值
        mStationID = "";
        Lotid = "";
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.zyDownLoad,R.id.zyrbA,R.id.zyrbB,R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                String lotid = etLotID.getText().toString();
                if (lotid.equals("")){
                    showToast("批次信息不能为空！",false);
                    return;
                }

                // 先清除文本框的内容
                zybdId.setText("");
                zyRecipeName.setText("");

                LotUtils.searchLotInfoStr(ZyRecipeActivity.this, lotid, getSupportFragmentManager(), new LotUtils.netCallback<StrzycxEntity>() {
                    @Override
                    public void onSuccess(StrzycxEntity response) {
                        etLotID.setText(lotid.toUpperCase());
                        Lotid = lotid.toUpperCase();
                        zybdId.setText(response.getRECIPEID());
                        zyRecipeName.setText(response.getRECIPENAME());
                        zyeqpWork.requestFocus();
                    }

                    @Override
                    public void onFail(Throwable t) {
                        //loadError(t, "LOTQUERY");
                    }
                });
                break;
            case R.id.zyDownLoad:
                String recipeid = zybdId.getText().toString();
                String recipename = zyRecipeName.getText().toString();
                if(recipeid.equals("") || recipename.equals("")){
                    showToast("请先查询程序信息",false);
                    return;
                }

                String eqpwork = zyeqpWork.getText().toString();
                if(eqpwork.equals("")){
                    showToast("请扫描测试设备编号",false);
                    return;
                }

                if (mStationID.equals("")){
                    showToast("请选择站点",false);
                    return;
                }

                JsonObject map = new JsonObject();
                map.addProperty("LOTID",Lotid);
                map.addProperty("RECIPEID",recipeid);
                map.addProperty("EQPID",eqpwork.toUpperCase());
                map.addProperty("STATION",mStationID);
                sendEqpRecipe(map);
                break;
            case R.id.zyrbA:
                mStationID = "StationA";
                break;
            case R.id.zyrbB:
                mStationID = "StationB";
                break;
        }
    }

    private void sendEqpRecipe(JsonObject map) {
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                ("下载程序中。。。",new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    showConfirmDialog(ZyRecipeActivity.this,"下载成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(),"CPC.StrCpcDownload");
            }
        };
        NetUtils.requestPostCPC("CPC.StrCpcDownload","StrCpcDownload", this,map,callBack);
    }

}
